package com.yc.bigdata.common.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import cn.hutool.core.date.format.FastDateFormat;

/**
 * <p></p>
 *
 * @author: YuanChilde
 * @date: 2020-02-15 9:13
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020-02-15 9:13    YuanChilde     1.0        新增
 */
public class NginxLogUtil {

    /**
     * 输入文件日期时间格式：13/Feb/2020:23:59:06 +0800
     */
    private final static FastDateFormat YYYYMMDDHHMM_TIME_FORMAT = FastDateFormat.getInstance("dd/MMM/yyyy:hh:mm:ss Z", Locale.ENGLISH);
    /**
     * 目标日期格式
     */
    private final static FastDateFormat TARGET_TIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    public static String parseTimeToNewFormat(String time) {
        return TARGET_TIME_FORMAT.format(parseTimeToDate(time));
    }

    public static Date parseTimeToDate(String time) {
        return new Date(parseTimeToLong(time));
    }

    public static Long parseTimeToLong(String time) {
        try {
            return YYYYMMDDHHMM_TIME_FORMAT.parse(time.substring(time.indexOf("[") + 1, time.lastIndexOf("]"))).getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }

    public static void main(String[] args) {
        String nginxTimeStr = "[13/Feb/2020:23:59:06 +0800]";
        System.out.println(parseTimeToNewFormat(nginxTimeStr));
    }
}
